using System;
using System.Collections.Generic;
using System.Linq;
using MNH.Yarekoodak.DomainClasses;
using MNH.Yarekoodak.DomainClasses.Repositories;
using MNH.WebApps.Common.Helpers.JqGrid;
using System.Collections.Specialized;
using MNH.WebApps.Common.Data;
using MNH.WebApps.Common.Extensions;

namespace MNH.Yarekoodak.Services.Pages
{
    /// <summary>
    /// Page service
    /// </summary>
    public partial class PageService : IPageService
    {
        #region Fields
        private readonly IRepository<Page> _PageRepository = new EfRepository<Page>();
        private readonly IRepository<Location> _locationRepository = new EfRepository<Location>();
        #endregion

        #region Ctor
        public PageService()
        {
        }

        #endregion

        #region Methods

        /// <summary>
        /// Deletes a Page 
        /// </summary>
        /// <param name="Page">Page </param>
        public virtual void DeletePage(Page Page)
        {
            if (Page == null)
                throw new ArgumentNullException("Page");

            _PageRepository.Delete(Page);
        }

        ///// <summary>
        ///// Gets a Page 
        ///// </summary>
        ///// <param name="PageId">Page  identifier</param>
        ///// <returns>Page </returns>
        public virtual Page GetPageById(int PageId, bool asNoTrack = false)
        {
            if (PageId == 0)
                return null;

            return _PageRepository.Table.Where(c => c.Id == PageId).SingleOrDefault();
        }


        public bool UpdatePagesOrder(IEnumerable<int> Ids)
        {
            try
            {
                int firstOrder = 1;
                foreach (var id in Ids)
                {
                    var page = this.GetPageById(id);
                    page.Priority = firstOrder;
                    firstOrder++;
                    this.UpdatePage(page);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public IEnumerable<Page> GetAllPages()
        {
            var pages = _PageRepository.Table;
            pages = pages.OrderBy(b => b.Priority);
            return pages;
        }

        public IPagedList<Page> GetAllPages(GridSettings gridSetting, NameValueCollection form)
        {
            var query = _PageRepository.Table;

            query = new JqGridSearch().ApplyFilter(query, gridSetting.IsSearch, gridSetting.SearchField, gridSetting.SearchString,
                      gridSetting.SearchOper, gridSetting.Filters, form);

            query = query.OrderBy(gridSetting.SortColumn + " " + gridSetting.SortOrder);
            //if (gridSetting.Operation == "excel")
            //{
            //    gridSetting.PageIndex = 1;
            //    gridSetting.PageSize = int.MaxValue;
            //}
            var pages = new PagedList<Page>(query, gridSetting.PageIndex, gridSetting.PageSize);
            return pages;
        }

        /// <summary>
        /// Inserts an Page 
        /// </summary>
        /// <param name="Page">Page </param>
        public virtual void InsertPage(Page Page)
        {
            if (Page == null)
                throw new ArgumentNullException("Page");

            _PageRepository.Insert(Page);
        }

        /// <summary>
        /// Updates the Page 
        /// </summary>
        /// <param name="Page">Page </param>
        public virtual void UpdatePage(Page Page)
        {
            if (Page == null)
                throw new ArgumentNullException("Page");

            _PageRepository.Update(Page);
        }


        #endregion
    }
}
